package com.lsfb.duishangbaos.Operate;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.lsfb.duishangbaos.R;
import com.lsfb.duishangbaos.Utils.LittleUtils;
import com.lsfb.duishangbaos.Utils.URLString;
import com.zgscwjm.lsfbbasetemplate.Event.EventAnnotation;
import com.zgscwjm.lsfbbasetemplate.Event.LsfbEvent;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;
import com.zgscwjm.lsfbbasetemplate.internet.BaseInternet;
import com.zgscwjm.lsfbbasetemplate.utils.SN;

import java.util.HashMap;

/**
 * 设置金额
 * Created by Administrator on 2017/5/10 0010.
 */
@ContentView(R.layout.aty_set_money)
public class SetMoneyActivity extends LsfbActivity {

    @ViewInject(R.id.aty_et_money)
    private EditText et_money;

    @Override
    public void initView() {
        LittleUtils.setsimpletitlebar(this, "设置金额");
    }

    @Override
    public void initEvent() {
        et_money.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable edt) {
                String temp = edt.toString();
                int posDot = temp.indexOf(".");
                if (posDot <= 0) return;
                if (temp.length() - posDot - 1 > 2) {
                    edt.delete(posDot + 3, posDot + 4);
                }
            }

            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }

            public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
            }
        });
    }


    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_btn_post})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.aty_btn_post:
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(et_money.getWindowToken(), 0);
                getData();
                break;
        }
    }

    @Override
    public void initData() {

    }

    public void getData() {
        if (et_money.getText().toString().isEmpty()) {
            SN.SnackShow(et_money, "请输入收款金额");
            return;
        }
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("cash", et_money.getText().toString());
        new BaseInternet().getData(URLString.SETCASH, hashMap, SetCashBean.class, new SetCashEvent<SetCashBean>(), true);
    }

    @EventAnnotation
    public void SetCashEvent(SetCashEvent<SetCashBean> event) {
        switch (event.getCode()) {
            case 200:

                SetMoneyClickEvent event1 = new SetMoneyClickEvent();
                event1.setData(event.getData().getKey());
                LsfbEvent.getInstantiation().post(event1);

                SetMoneyClickEvent2 event2 = new SetMoneyClickEvent2();
                event2.setData(et_money.getText().toString());
                LsfbEvent.getInstantiation().post(event2);

                finish();
                break;
            default:
                SN.SnackShow(et_money, event.getMsg());
                break;
        }
    }


}
